Skip to content

Reduce background Git ref and port polling#4187

Open
Quicksaver wants to merge 7 commits into
pingdotgg:mainfrom
Quicksaver:fix/reduce-background-git-ref-port-polling
Open

Reduce background Git ref and port polling#4187
Quicksaver wants to merge 7 commits into
pingdotgg:mainfrom
Quicksaver:fix/reduce-background-git-ref-port-polling

Conversation

@Quicksaver

@Quicksaver Quicksaver commented Jul 20, 2026

Copy link
Copy Markdown

Summary

Reduce persistent background work from Git ref enumeration and preview port discovery while preserving freshness when users interact with either surface.

What Changed

  • Revalidate only the first Git-ref page every 20 seconds instead of polling every retained page every five seconds
  • Load cursor pages once per connected generation and expire inactive ref atoms after 30 seconds
  • Refresh local and remote refs immediately when the composer branch or Diff comparison menu opens
  • Replay the latest preview-discovery snapshot instead of running a duplicate subscription scan
  • Serialize snapshot replay with concurrent broadcasts so subscribers cannot regress to stale results
  • Scan immediately when managed terminal process sets change and ignore unchanged registrations or redundant removals
  • Run the broad port-discovery safety net every 20 seconds while idle and every 10 seconds when a listener is known

Why

During extended use, T3 Code's resident memory climbed past 6 GB even while idle, while the renderer sustained roughly 10% CPU and the machine became noticeably warm around the CPU and GPU clusters. That pattern indicated that background work was continuing without user interaction and could compound renderer and resource pressure over long sessions.

Diagnostics identified two repeatable sources of idle churn: every retained Git-ref query, including each loaded pagination page, owned its own five-second listRefs revalidation loop, while preview discovery launched a broad lsof scan every three seconds. Preview subscription initialization could also scan twice, and unchanged terminal process registrations caused redundant probes. The new schedules reduce that steady subprocess load while immediate menu refreshes, terminal-process-triggered scans, and adaptive safety-net polling preserve freshness at interaction time.

Validation

  • Port scanner focused suite passed with 6 tests, including concurrent replay ordering
  • Targeted server and client-runtime type checks passed
  • Isolated authenticated web verification loaded local, worktree, and remote refs when the branch selector opened
  • git diff --check passed

Proof

No standalone proof artifacts were captured; the isolated browser verification is summarized above.


Note

Medium Risk
Changes timing and ordering of preview discovery and Git ref freshness paths that many UI surfaces depend on; regressions could show stale ports/refs or missed updates, but scope is bounded and covered by new port-scanner tests.

Overview
Reduces steady background subprocess and RPC churn from Git ref listing and preview port discovery, aimed at lowering idle CPU/memory use during long sessions.

Git refs (vcs.ts) — First-page ref lists revalidate every 20s (was 5s). Inactive ref atoms expire after 30s (was 5 minutes). Paginated/cursor listRefs queries no longer tick on an interval; they fetch once per connected generation.

UI (DiffPanel.tsx) — Opening the branch comparison ref combobox calls refresh() on local and remote ref queries so the menu is up to date without waiting for the slower background interval.

Preview ports (PortScanner.ts, ws.ts) — Replaces a fixed 3s lsof/probe loop with 10s when a listener is known and 20s when idle or empty. retain still triggers an immediate scan when discovery was idle; subscribe replays lastSnapshot under a semaphore so replay and broadcasts cannot reorder. Unchanged terminal PID registrations and no-op unregisters skip extra scans; real changes still pollTick immediately. WebSocket subscribeDiscoveredLocalServers drops the extra scan() on subscribe and relies on retain + subscribe replay.

Tests — Adds coverage for unchanged registration deduping and ordered delivery under concurrent replay/broadcast.

Docs — Adds BRANCH_DETAILS.md describing expected behavior and dev ports.

Reviewed by Cursor Bugbot for commit 8f2fdbb. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Reduce background Git ref and port polling frequency

  • Increases Git ref revalidation interval from 5s to 20s and sets idle atom TTL to 30s; paginated/cursor pages are now one-shot fetches with no periodic revalidation (vcs.ts)
  • Replaces the fixed 3s port scan loop with an adaptive loop sleeping 10s when active and 20s when idle or no servers are known (PortScanner.ts)
  • Serializes snapshot broadcasts and subscriber replay via a Semaphore, preventing out-of-order delivery and stale replays after newer scans
  • Skips polling when terminal registrations or unregistrations are unchanged, triggering immediate scans only on actual changes
  • Opening the comparison-ref menu in DiffPanel now triggers an immediate refresh of local and remote branch refs

Macroscope summarized 8f2fdbb.

- Poll first ref pages every 20 seconds and refresh menus on open
- Avoid duplicate port scans and adapt lsof polling to activity
- Document the retained freshness guarantees
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 52d26e0f-e3fd-421e-9844-29ada1eeec35

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list. labels Jul 20, 2026
Comment thread apps/server/src/preview/PortScanner.ts
@macroscopeapp

macroscopeapp Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Needs human review

This PR significantly modifies polling intervals and subscription notification mechanics, changing how data freshness is managed throughout the application. The new semaphore-based serialization and state-dependent polling represent meaningful behavioral changes that warrant human review.

You can customize Macroscope's approvability policy. Learn more.

- Order subscription replay with concurrent snapshot broadcasts
- Cover replay ordering with a focused concurrent regression test
- Document the preview notification ordering guarantee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant